package com.pda.common.pages.login;

import android.content.Context;
import com.apkfuns.logutils.LogUtils;
import com.pda.common.basemvp.model.BaseModel;
import com.pda.common.bean.BaseBean;
import com.pda.common.bean.LoginBean;
import com.pda.common.beanin.LoginInBean;
import com.pda.common.tools.SharedPreferencesManger;
import com.pda.common.tools.StringProcess;
import com.pda.common.utils.HttpConfig;
import com.pda.common.utils.PDAUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class LoginModelImpl extends BaseModel implements LoginModel {
    private String TAG = "LoginModelImpl";
    private BaseBean baseBean=null;
    private LoginBean loginBean=null;

    public LoginModelImpl(Context context) {
        super(context);
    }

    @Override
    public void login(final String uname, final String password, final CallBack callBack) {
        try {
            httpConfig = new Retrofit.Builder()
                    .baseUrl(PDAUtil.hostname)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(client)
                    .build().create(HttpConfig.class);
            final LoginInBean loginInBean = new LoginInBean();
            loginInBean.setAction("Login");
            loginInBean.setCid(SharedPreferencesManger.getCid());
            loginInBean.setUserName(uname);
            loginInBean.setPassword(password);
//            Misc misc = new Misc();
//            loginInBean.setSn(misc.getSN());
            loginInBean.setSn("dealerweb");
            RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(loginInBean));
            httpConfig.login(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.onLoginSuccess(loginBean.getData().getShowInfo());
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (baseBean.getErrmsg() != null && baseBean.getErrmsg().length() > 0) {
                                callBack.onLoginFail(baseBean.getErrmsg());
                            } else {
                                callBack.onLoginFail(PDAUtil.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            baseBean = gson.fromJson(StringProcess.responsetoString(abBean), BaseBean.class);
                            if (baseBean != null && baseBean.getErrcode() == 0) {
                                loginBean = gson.fromJson(StringProcess.responsetoString(abBean), LoginBean.class);
                                SharedPreferencesManger.setUname(uname);
                                SharedPreferencesManger.setUpass(password);
                                SharedPreferencesManger.setUID(loginBean.getData().getUserID()+"");
                                SharedPreferencesManger.setStoreID(loginBean.getData().getStoreID());
                                SharedPreferencesManger.setStoreCode(loginBean.getData().getStoreCode());
                                SharedPreferencesManger.setStoreName(loginBean.getData().getStoreName());
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.onLoginFail(PDAUtil.connectError);
            e.printStackTrace();
        }
    }
}

